package com.og.managedbean;

import java.io.Serializable;

import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.log4j.Logger;

import com.og.backingbean.Application;
import com.og.backingbean.ApplicationHistory;
import com.og.backingbean.Type;
import com.og.dao.CRUDEntityDAO;

/**
 * @author Renaud DIANA
 *
 */
@Named
@SessionScoped
public class ApplicationBean implements Serializable {

	private static final long serialVersionUID = 1L;
	
	// Log4j	 
	private static final Logger	log	= Logger.getLogger(ApplicationBean.class);

	/* Dependency injection */
	// Model
    @Inject
    private CRUDEntityDAO<Application> applicationDAO;
    @Inject
    private CRUDEntityDAO<ApplicationHistory> applicationHistoryDAO;
    // BackingBean    
    @Inject
    private Type type;
    @Inject
    private Application applicationNew;
    @Inject
    private Application applicationUpdate;
    @Inject
    private Application applicationView;
    @Inject
    private ApplicationHistory applicationHistory;
    @Inject
    private ListBean list;
        
    /* -------------------- */
    
	/**
	 * Default constructor
	 */
	public ApplicationBean() {
		// TODO Auto-generated constructor stub
	}

	public String create() {
		
		// Call model		
		applicationDAO.createEntity(applicationNew);
		
		// Generate new list
		list.findAllApplication();
		
		// Clear form
		applicationNew = new Application();
						
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create application Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}	
	
	public String update() {
						
		// Check version for history
		if(!applicationUpdate.getVersion().equals(applicationHistory.getVersion())) {
			
			// Set applicationHistory
			applicationHistory.setApplication(applicationUpdate);
			applicationHistory.setDate(applicationUpdate.getDate());			
			applicationHistory.setPrice(applicationUpdate.getPrice());
			
			// Call model
			applicationHistoryDAO.createEntity(applicationHistory);
								
			// Reset form
			applicationHistory = new ApplicationHistory();
			
			// Generate new list
			list.findAllApplicationHistory();
						
			// Add message
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Create applicationHistory Successful"));
		}
		
		// Call model				
		applicationDAO.updateEntity(applicationUpdate);
		
		// Toggle view
		applicationView = applicationUpdate;
				
		// Generate new list
		list.findAllApplication();
			
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Update application Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String delete() {
				
		// Call model				
		applicationDAO.deleteEntity(applicationView);
		
		// Generate new list
		list.findAllApplication();
		
		// Clear form
		applicationView = new Application();
				
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Delete application Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}
	
	public String findOne() {
		
		// Call model				
		applicationView = applicationDAO.findOneEntity(applicationView, applicationView.getIdApplication());
					
		// Add message
		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Find application Successful"));  
		
		// Navigation case
		return "";
		//return "success";
	}

	public String toggleUpdate() {
		
		applicationUpdate = applicationView;
		applicationHistory = new ApplicationHistory();
		applicationHistory.setVersion(applicationView.getVersion());
		
		return "";
	}
	
	// Getters and Setters
	
	public Type getType() {
		return type;
	}

	public void setType(Type type) {
		this.type = type;
	}

	public Application getApplicationNew() {
		return applicationNew;
	}

	public void setApplicationNew(Application applicationNew) {
		this.applicationNew = applicationNew;
	}

	public Application getApplicationUpdate() {
		return applicationUpdate;
	}

	public void setApplicationUpdate(Application applicationUpdate) {
		this.applicationUpdate = applicationUpdate;
	}

	public Application getApplicationView() {
		return applicationView;
	}

	public void setApplicationView(Application applicationView) {
		this.applicationView = applicationView;
	}

	public ApplicationHistory getApplicationHistory() {
		return applicationHistory;
	}

	public void setApplicationHistory(ApplicationHistory applicationHistory) {
		this.applicationHistory = applicationHistory;
	}

}
